//
//  QZSliderView.m
//  SliderView_0cccccccc
//
//  Created by xalo on 16/7/11.
//  Copyright © 2016年 xmaz. All rights reserved.
//

#import "QZSliderView.h"
#import <UIImageView+WebCache.h>

#define kWidth self.frame.size.width
#define kHeight self.frame.size.height

@interface QZSliderView ()<UIScrollViewDelegate>

@property(nonatomic,strong)UIScrollView *scrollView;

@property(nonatomic,strong)UIImageView *leftImageView;
@property(nonatomic,strong)UIImageView *centerImageView;
@property(nonatomic,strong)UIImageView *rightImageView;

@property(nonatomic,strong)CAShapeLayer *shape;//小滑动条

@property(nonatomic,strong)NSArray *imageArray;//存放图片网址字符串的数组

@property(nonatomic,assign)NSInteger index;//向左划时 rightImage 的下标
@property(nonatomic,assign)NSInteger subIndex; //向右划时 leftImage 的下标
@property(nonatomic,assign)NSInteger shapeIndex; //shape 的下标

@property(nonatomic,strong)NSTimer *timer; //定时器
@end


@implementation QZSliderView

- (instancetype)initWithFrame:(CGRect)frame AndImageArray:(NSArray *)imageArray{
    self = [super initWithFrame:frame];
    if (self) {
        self.imageArray = imageArray;
        [self.scrollView addSubview:self.leftImageView];
        [self.scrollView addSubview:self.centerImageView];
        [self.scrollView addSubview:self.rightImageView];
        [self addSubview:self.scrollView];
        [self.layer addSublayer:self.shape];
        //两秒后开启定时器
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            self.timer.fireDate = [NSDate distantPast];
        });
        self.index = 2;
        self.subIndex = self.imageArray.count - 2;
    }
    return self;
}
//属性懒加载
- (UIScrollView *)scrollView{
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
        _scrollView.pagingEnabled = YES;
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.contentSize =CGSizeMake(kWidth * 3, 0);
        _scrollView.contentOffset = CGPointMake(kWidth, 0);
        _scrollView.bounces = NO;
        _scrollView.delegate = self;
    }
    return _scrollView;
}

- (UIImageView *)leftImageView{
    if (!_leftImageView) {
        _leftImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight - 4)];
        [_leftImageView sd_setImageWithURL:[NSURL URLWithString:self.imageArray[self.imageArray.count - 1]]];
    }
    return _leftImageView;
}
- (UIImageView *)centerImageView{
    if (!_centerImageView) {
        _centerImageView = [[UIImageView alloc] initWithFrame:CGRectMake(kWidth, 0, kWidth, kHeight - 4)];
        [_centerImageView sd_setImageWithURL:[NSURL URLWithString:self.imageArray[0]]];
    }
    return _centerImageView;
}
- (UIImageView *)rightImageView{
    if (!_rightImageView) {
        _rightImageView = [[UIImageView alloc] initWithFrame:CGRectMake(kWidth * 2, 0, kWidth, kHeight - 4)];
        [_rightImageView sd_setImageWithURL:[NSURL URLWithString:self.imageArray[1]]];
    }
    return _rightImageView;
}
- (CAShapeLayer *)shape{
    if (!_shape) {
        UIBezierPath *path = [[UIBezierPath alloc] init];
        [path moveToPoint:CGPointMake(0, kHeight - 2)];
        [path addLineToPoint:CGPointMake(kWidth, kHeight - 2)];
        _shape = [[CAShapeLayer alloc] init];
        _shape.path = path.CGPath;
        _shape.lineWidth = 4;
        _shape.strokeEnd = 1.0 / self.imageArray.count;
        _shape.strokeColor = [UIColor colorWithRed:0.00 green:0.73 blue:0.61 alpha:1.00].CGColor;
    }
    return _shape;
}
- (NSTimer *)timer{
    if (!_timer) {
        _timer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
    }
    return _timer;
}
#pragma mark delegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    if (scrollView.contentOffset.x == 2 * kWidth) {
        self.shapeIndex++;
        if (self.shapeIndex == self.imageArray.count) {
            self.shapeIndex = 0;
        }
        self.shape.strokeStart = 1.0 / self.imageArray.count * self.shapeIndex;
        self.shape.strokeEnd = 1.0 / self.imageArray.count * (self.shapeIndex + 1);
        
        
        
        scrollView.contentOffset = CGPointMake(kWidth, 0);
        if (self.index == self.imageArray.count) {
            self.index = 0;
        }
        self.leftImageView.image = self.centerImageView.image;
        self.centerImageView.image = self.rightImageView.image;
        [self.rightImageView sd_setImageWithURL:[NSURL URLWithString:self.imageArray[self.index]]];
        
        self.index++;
        self.subIndex++;
        if (self.subIndex == self.imageArray.count) {
            self.subIndex = 0;
        }
        
    }
    if (scrollView.contentOffset.x == 0) {
        
        self.shapeIndex--;
        if (self.shapeIndex == -1) {
            self.shapeIndex = self.imageArray.count - 1;
        }
        self.shape.strokeStart = 1.0 / self.imageArray.count * self.shapeIndex;
        self.shape.strokeEnd = 1.0 / self.imageArray.count * (self.shapeIndex + 1);
        
        
        scrollView.contentOffset = CGPointMake(kWidth, 0);
        
        if (self.subIndex == -1) {
            self.subIndex = self.imageArray.count - 1;
        }
        self.rightImageView.image = self.centerImageView.image;
        self.centerImageView.image = self.leftImageView.image;
        [self.leftImageView sd_setImageWithURL:[NSURL URLWithString:self.imageArray[self.subIndex]]];
        self.subIndex--;
        self.index--;
        if (self.index == -1) {
            self.index = self.imageArray.count - 1;
        }
    }
}
#pragma mark  timer Action
- (void)timerAction{
    [self.scrollView setContentOffset:CGPointMake(self.scrollView.contentOffset.x + kWidth, 0) animated:YES];
}


@end
